package com.project.myPackage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.myPackage.common.R;
import com.project.myPackage.entity.Favorites;
import com.project.myPackage.service.FavoritesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "收藏管理")
@Slf4j
@RestController
@RequestMapping("/")
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;

    @ApiOperation(value = "根据用户id和商品id判断是否收藏该商品")
    @RequestMapping("/isFavorite")
    public R<?> isFavorite(Integer user_id, Integer product_id)
    {
        LambdaQueryWrapper<Favorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorites::getUser_id, user_id);
        queryWrapper.eq(Favorites::getProduct_id, product_id);
        Favorites favorites = favoritesService.getOne(queryWrapper);
        if (favorites!=null) {
            return R.isFavorite("已收藏", true);

        } else {
            return R.isFavorite("未收藏", false);
        }
    }

    @ApiOperation(value = "根据用户id获取所有收藏信息")
    @RequestMapping("/getAllFavoritesByUserId")
    public R<?> getAllFavoritesByUserId(Integer user_id)
    {
        List<Map<String, Object>> list = favoritesService.getList(user_id);
      if(list!=null){
          return R.success(list,"获取所有收藏信息列表成功！");
      }else {
          return R.error("获取所有收藏信息列表失败！");
      }
    }

    @ApiOperation(value = "新增收藏信息")
    @PostMapping("/addFavorites")
    public R<?> addFavorites(@RequestBody Favorites favorites)
    {
        LambdaQueryWrapper<Favorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorites::getUser_id, favorites.getUser_id());
        queryWrapper.eq(Favorites::getProduct_id, favorites.getProduct_id());
        List<Favorites> list = favoritesService.list(queryWrapper);
        if(list.size() == 0){
            boolean save = favoritesService.save(favorites);
            if (save) {
                return R.success("收藏商品成功！");
            } else {
                return R.error("收藏商品失败！");
            }
        }else{
            return R.error("该用户已经收藏过该商品！");
        }
    }

//    @ApiOperation(value = "取消/删除收藏信息")
//    @RequestMapping("/deleteFavorites")
//    public R<?> deleteFavorites(Integer favorite_id)
//    {
//        boolean remove = favoritesService.removeById(favorite_id);
//        if (remove) {
//            return R.success("取消收藏成功！");
//        }else{
//            return R.error("取消收藏失败！");
//        }
//    }

    @ApiOperation(value = "取消/删除收藏信息")
    @PostMapping("/deleteFavorites")
    public R<?> deleteFavorites(@RequestBody Favorites favorites)
    {
        LambdaQueryWrapper<Favorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorites::getProduct_id, favorites.getProduct_id());
        queryWrapper.eq(Favorites::getUser_id, favorites.getUser_id());

        Favorites favorite = favoritesService.getOne(queryWrapper);
        if(favorite!=null){
            boolean remove = favoritesService.removeById(favorite.getFavorite_id());
            if (remove) {
                return R.success("取消收藏成功！");
            }else{
                return R.error("取消收藏失败！");
            }
        }else{
            return R.error("收藏信息不存在！");
        }


    }

    @ApiOperation(value = "删除收藏信byId")
    @GetMapping("/deleteFavoritesById")
    public R<?> deleteFavoritesById(Integer favorite_id)
    {
        boolean remove = favoritesService.removeById(favorite_id);
        if (remove) {
            return R.success("取消收藏成功！");
        }else{
            return R.error("取消收藏失败！");
        }
    }

}
